package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.Help;
import org.example.system.service.HelpService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 求助控制层
 */
@RestController
@RequestMapping("/app/help")
@Slf4j
public class HelpController {

    @Autowired
    private HelpService helpService;

    /**
     * 按条件分页查询
     * @param help
     * @return
     */
    @GetMapping("/list")
    public R list(Help help){
        IPage<Help> page = helpService.selectPage(help);
        log.info("------------ HelpController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }
    /**
     * 添加求助
     * @param help
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Help help, HttpServletRequest request){
        log.info("--------- HelpController | add | input : {}", JSON.toJSON(help));
        int row = helpService.add(help);
        if(row <= 0){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param help
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Help help,HttpServletRequest request){
        log.info("--------- HelpController | update | input : {}", JSON.toJSON(help));
        if(null == help.getId() || help.getId() <= 0){
            return R.fail("参数错误:ID未传入");
        }
        int row = helpService.update(help);
        if(row <= 0){
            return  R.fail("修改失败");
        }
        return R.ok("修改成功");
    }

    /**
     * 根据id查询求助数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- HelpController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(helpService.getOne(id));
    }
}
